package com.glory.wxelectricitypda.activity;

import android.os.Handler;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.glory.wxelectricitypda.R;
import com.glory.wxelectricitypda.adapter.SameWorkorderlotsAdapter;
import com.glory.wxelectricitypda.adapter.UnSameWorkorderlotsAdapter;
import com.glory.wxelectricitypda.base.BaseDialogActivity;
import com.glory.wxelectricitypda.bean.BACKShelfBean;
import com.glory.wxelectricitypda.bean.ItemShelfEntity;
import com.glory.wxelectricitypda.bean.ResponseBean;
import com.glory.wxelectricitypda.utils.NetUtils;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.lzy.okgo.model.Response;
import com.pda.platform.ui.ui_pdaplatform.callback.FreeUI_DialogEntityCallBack;
import com.pda.platform.ui.ui_pdaplatform.entity.FreeUI_AddViewEntity;
import com.pda.platform.ui.ui_pdaplatform.view.FreeUI_ClearEditText;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

public class ItemUnShelfActivity extends BaseDialogActivity {

    @BindView(R.id.etLotID)
    FreeUI_ClearEditText etLotID;
    @BindView(R.id.rvSameWorkorderlotsList)
    RecyclerView rvSameWorkorderlotsList;
    @BindView(R.id.rvUnSameWorkorderlotsList)
    RecyclerView rvUnSameWorkorderlotsList;
    @BindView(R.id.tvLotQty)
    TextView tvLotQty;
    @BindView(R.id.tvSubstrateId1)
    TextView tvSubstrateId1;
    @BindView(R.id.tvLocationID)
    TextView tvLocationID;
    @BindView(R.id.rbYes)
    RadioButton rbYes;
    @BindView(R.id.rbUnYes)
    RadioButton rbUnYes;
    @BindView(R.id.llMain)
    LinearLayout llMain;
    @BindView(R.id.isLotID)
    FreeUI_ClearEditText isLotID;
    @BindView(R.id.isUnLotID)
    FreeUI_ClearEditText isUnLotID;

    private SameWorkorderlotsAdapter mSameWorkorderlotsAdapter;
    private UnSameWorkorderlotsAdapter unSameWorkorderlotsAdapter;
    private BACKShelfBean.PARENTLOTDTO parentlot;
    private List<BACKShelfBean.TESTCLOTSDTO> test_lots;
    private String woid;

    @Override
    protected int getLayoutResId() {
        return R.layout.activity_itemunshelf;
    }

    @Override
    protected void initView() {
        rvSameWorkorderlotsList.setLayoutManager(new LinearLayoutManager(ItemUnShelfActivity.this));
        rvSameWorkorderlotsList.setItemAnimator(new DefaultItemAnimator());
        rvSameWorkorderlotsList.addItemDecoration(new DividerItemDecoration(ItemUnShelfActivity.this, 1));
        mSameWorkorderlotsAdapter = new SameWorkorderlotsAdapter(null);
        mSameWorkorderlotsAdapter.openLoadAnimation(BaseQuickAdapter.ALPHAIN);
        mSameWorkorderlotsAdapter.isFirstOnly(true);
        rvSameWorkorderlotsList.setAdapter(mSameWorkorderlotsAdapter);

        rvUnSameWorkorderlotsList.setLayoutManager(new LinearLayoutManager(ItemUnShelfActivity.this));
        rvUnSameWorkorderlotsList.setItemAnimator(new DefaultItemAnimator());
        rvUnSameWorkorderlotsList.addItemDecoration(new DividerItemDecoration(ItemUnShelfActivity.this, 1));
        unSameWorkorderlotsAdapter = new UnSameWorkorderlotsAdapter(null);
        unSameWorkorderlotsAdapter.openLoadAnimation(BaseQuickAdapter.ALPHAIN);
        unSameWorkorderlotsAdapter.isFirstOnly(true);
        rvUnSameWorkorderlotsList.setAdapter(unSameWorkorderlotsAdapter);
    }

    @Override
    protected void initData() {

        initEdit(etLotID, s -> {
            itemShelfQuery(s);
        });

        freeUI_titleView.setRightListener("下架", new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (etLotID.getText().toString().equals("")){
                    showToast("请扫描批次号",false);
                    return;
                }
                boolean type = false;
                for (int i =0;i<test_lots.size();i++){
                    if (test_lots.get(i).getWOID().equals(woid) && test_lots.get(i).getUDF().getENDMARK().equals("C")){
                        if (!unSameWorkorderlotsAdapter.getLotList().contains(i)){
                            type = true;
                            break;
                        }
                    }
                }
                if (type){
                    showToast("请勾选与主批工单相同的尾数批",false);
                    return;
                }
                submin();
            }
        });

        initEdit(isLotID,s ->{
            mSameWorkorderlotsAdapter.setLotid(s);
            mSameWorkorderlotsAdapter.notifyDataSetChanged();
            showToast("已扫描",true);
        });
        initEdit(isUnLotID,s ->{
            unSameWorkorderlotsAdapter.setLotid(s);
            unSameWorkorderlotsAdapter.notifyDataSetChanged();
            showToast("已扫描",true);
        });
    }

    private void itemShelfQuery(String s) {
        mSameWorkorderlotsAdapter.setNewData(null);
        mSameWorkorderlotsAdapter.getLotList().clear();
        mSameWorkorderlotsAdapter.setLotid("");
        unSameWorkorderlotsAdapter.setNewData(null);
        unSameWorkorderlotsAdapter.getLotList().clear();
        unSameWorkorderlotsAdapter.setLotid("");
        JSONObject map = new JSONObject();
        JSONObject lotid = new JSONObject();
        JSONObject jsonObject = new JSONObject();
        try {
            map.put("ACTIONTYPE", "itemOnShelfQuery");
            map.put("LOCATIONID", "");
            lotid.put("LOTID",s);
            map.put("PARENTLOT",lotid);
//            ArrayList<MLotBean> lotBeans = new ArrayList<>();
//            MLotBean mLotBean = new MLotBean();
//            mLotBean.setLOTID(etLotID.getText().toString());
//            lotBeans.add(mLotBean);
//            JSONArray jsonArray = new JSONArray(new Gson().toJson(lotBeans));
//            map.put("LOTLIST", jsonArray);
            jsonObject.put("Data", map);
            jsonObject.put("MessageName", "EUIT.TESTBANK");
        } catch (JSONException e) {
            e.printStackTrace();
        }
        NetUtils.requestPostMesForwardUtils(jsonObject, this, new FreeUI_DialogEntityCallBack<ResponseBean<BACKShelfBean>>(
                new TypeToken<ResponseBean<BACKShelfBean>>() {
                }.getType(), getSupportFragmentManager(), this) {
            @Override
            public void onSuccess(Response<ResponseBean<BACKShelfBean>> response) {
                if (response.body().isSuccess(getApplicationContext(), true, getSupportFragmentManager())) {
                    if (response.body().getBody().getPARENTLOT()!=null){
                        parentlot = response.body().getBody().getPARENTLOT();
                        tvLotQty.setText(String.valueOf(response.body().getBody().getPARENTLOT().getMAINQTY()));
                        tvSubstrateId1.setText(response.body().getBody().getPARENTLOT().getSUBSTRATEID1());
                        tvLocationID.setText(response.body().getBody().getPARENTLOT().getLOCATORID());
                    }else {
                        parentlot = null;
                        tvLotQty.setText("");
                        tvSubstrateId1.setText("");
                        tvLocationID.setText("");
                    }
                    mSameWorkorderlotsAdapter.setNewData(response.body().getBody().getTEST_LOTS());
                    test_lots = response.body().getBody().getTEST_C_LOTS();
                    woid = response.body().getBody().getPARENTLOT().getWOID();
                    unSameWorkorderlotsAdapter.setNewData(response.body().getBody().getTEST_C_LOTS());
                }
            }

            @Override
            public void onError(Response<ResponseBean<BACKShelfBean>> response) {
                super.onError(response);
                loadError(response.getException(), "EUIT.TESTBANK");
            }
        });
    }

    private void submin() {

        JSONObject map = new JSONObject();
        JSONObject lotid = new JSONObject();
        JSONObject jsonObject = new JSONObject();
        try {
            map.put("ACTIONTYPE", "itemUnshelf");
            map.put("LOCATIONID", tvLocationID.getText().toString());
            map.put("ISTESTLOTSMERGE", rbYes.isChecked()?"Y":"N");
            map.put("ISTESTCLOTSMERGE", rbUnYes.isChecked()?"Y":"N");
            lotid.put("LOTID",etLotID.getText().toString());
            map.put("PARENTLOT",lotid);
            ArrayList<BACKShelfBean.TESTLOTSDTO> lotBeans = new ArrayList<>();
            ArrayList<BACKShelfBean.TESTCLOTSDTO> lotCBeans = new ArrayList<>();
            for (int i = 0; i < mSameWorkorderlotsAdapter.getLotList().size(); i++) {
                lotBeans.add(mSameWorkorderlotsAdapter.getData().get(mSameWorkorderlotsAdapter.getLotList().get(i)));
            }
            for (int i = 0; i < unSameWorkorderlotsAdapter.getLotList().size(); i++) {
                lotCBeans.add(unSameWorkorderlotsAdapter.getData().get(unSameWorkorderlotsAdapter.getLotList().get(i)));
            }
            JSONArray jsonArray = new JSONArray(new Gson().toJson(lotBeans));
            JSONArray jsonCArray = new JSONArray(new Gson().toJson(lotCBeans));
            map.put("TESTLOTS", jsonArray == null?"":jsonArray);
            map.put("TESTCLOTS", jsonCArray == null?"":jsonCArray);
            JSONObject jsonparentlotArray = new JSONObject(new Gson().toJson(parentlot));
            map.put("PARENTLOT",jsonparentlotArray);
            jsonObject.put("Data", map);
            jsonObject.put("MessageName", "EUIT.TESTBANK");
        } catch (JSONException e) {
            e.printStackTrace();
        }
        NetUtils.requestPostMesForwardUtils(jsonObject, this, new FreeUI_DialogEntityCallBack<ResponseBean<ItemShelfEntity>>(
                new TypeToken<ResponseBean<ItemShelfEntity>>() {
                }.getType(), getSupportFragmentManager(), this) {
            @Override
            public void onSuccess(Response<ResponseBean<ItemShelfEntity>> response) {
                if (response.body().isSuccess(getApplicationContext(), true, getSupportFragmentManager())) {
                    showToast("下架成功",true);
                    new Handler().postDelayed(new Runnable() {
                        @Override
                        public void run() {
                            finish();
                        }
                    }, 2000);
                }
            }

            @Override
            public void onError(Response<ResponseBean<ItemShelfEntity>> response) {
                super.onError(response);
                loadError(response.getException(), "EUIT.TESTBANK");
            }
        });
    }

    @Override
    protected FreeUI_AddViewEntity getBaseEntity() {
        freeUI_titleView.setTitle("BANK下架");
        return new FreeUI_AddViewEntity("header", llMain);
    }
}

